<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="dialogFormVisible=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table v-loading="loading" border="" :data="roleList.rows">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="editPermission(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="roleList.total"
                :page-size="pageConfig.pagesize"
                :current-page="pageConfig.page"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyList.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyList.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyList.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyList.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

        <!-- 表单对话框 -->
        <el-dialog :title="form.id? '编辑角色' :'新增角色'" :visible.sync="dialogFormVisible" @close="cleanUpData">
          <el-form ref="form" :model="form" label-width="auto" :rules="rules">
            <el-form-item label="角色名" prop="name">
              <el-input v-model="form.name" autocomplete="off" placeholder="角色名2~9字符" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="form.description" autocomplete="off" placeholder="角色描述1~50字符" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cleanUpData">取 消</el-button>
            <el-button type="primary" @click="sureOperate">确 定</el-button>
          </div>
        </el-dialog>

        <!-- 分配权限对话框 -->
        <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
          <el-tree
            ref="permission"
            :data="permissionList"
            :props="{label:'name'}"
            :show-checkbox="true"
            :check-strictly="true"
            :default-expand-all="true"
            node-key="id"
          />
          <!-- 确定 取消 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
              <el-button size="small" @click="btnPermCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
// 角色管理API
import { getCompanyAPI, getRoleAPI, delRoleAPI, addRoleAPI, editRoleAPI, getIdRoleAPI, assignPremAPI } from '@/api/setting'
// 权限点API
import { getPermissionAPI } from '@/api/permission'
// 其他API
import { listToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      loading: false, // 加载
      dialogFormVisible: false,
      companyList: {}, // 公司数据
      roleList: [], // 角色数据
      pageConfig: {
        page: 1,
        pagesize: 3
      },
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      showPermDialog: false, // 权限对话框隐藏
      permissionList: [], // 权限列表
      roleId: ''
    }
  },
  async created() {
    // 请求企业数据
    this.companyList = await getCompanyAPI(this.$store.state.user.userInfo.companyId)
    this.getRole()
    this.getPermission()
  },
  methods: {
    // 获取角色数据
    async getRole(page) {
      this.loading = true
      this.roleList = await getRoleAPI(page)
      this.loading = false
    },
    // 角色页面翻页
    currentChange(newPage) {
      this.pageConfig.page = newPage
      this.getRole(newPage)
    },
    // 删除角色
    async delRole(id) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delRoleAPI(id)
      this.$message.success('删除成功')
      const num = Math.ceil(this.roleList.total / this.pageConfig.pagesize)
      this.currentChange(this.roleList.total % this.pageConfig.pagesize === 1 && this.pageConfig.page === num ? this.pageConfig.page - 1 : this.pageConfig.page)
    },
    // 清理表单
    cleanUpData() {
      this.dialogFormVisible = false
      this.$refs.form.resetFields()
    },
    // 确认操作
    async sureOperate() {
      await this.$refs.form.validate()
      if (this.form.id) {
        // 编辑角色
        await editRoleAPI(this.form)
        this.$message.success('编辑成功')
        await this.currentChange(this.pageConfig.page)
      } else {
        // 添加角色
        await addRoleAPI(this.form)
        this.$message.success('添加成功')
        await this.getRole()
        // 跳转到最后
        this.currentChange(Math.ceil(this.roleList.total / this.pageConfig.pagesize))
      }
      // 清理
      this.cleanUpData()
    },
    // 编辑表单显示
    editRole(row) {
      this.form = row
      this.dialogFormVisible = true
    },

    // 权限分配操作
    // 请求数据渲染
    async getPermission() {
      this.permissionList = await getPermissionAPI()
      this.permissionList = listToTreeData(this.permissionList, '0')
    },
    // 确定操作
    async btnPermOK() {
      // 发送请求
      await assignPremAPI({
        id: this.roleId,
        permIds: this.$refs.permission.getCheckedKeys()
      })
      this.$message.success('修改成功')
      // 清除选中数据
      this.$refs.permission.setCheckedKeys([])
      this.showPermDialog = false
    },
    // 取消操作
    btnPermCancel() {
      this.$refs.permission.setCheckedKeys([])
      this.showPermDialog = false
    },
    // 显示分配权限及回显
    async editPermission(id) {
      this.roleId = id
      const { permIds } = await getIdRoleAPI(id)
      this.showPermDialog = true
      // 等待对话框出来后赋值
      this.$nextTick(() => {
        this.$refs.permission.setCheckedKeys(permIds)
      })
    }
  }
}
</script>

<style>

</style>
